import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class PayService extends BaseService<any> {
  private readonly logger = new Logger(PayService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * pay
   * 对应 PHP: PayService_api::pay()
   * 逻辑类型: undefined - undefined
   */
  async pay(type: number, trade_type: number, trade_id: number, return_url: number, quit_url: number, buyer_id: number, voucher: number, openid: number) {
    // 基于PHP真实逻辑: pay
    // PHP原文: $member = (new CoreMemberService())->getInfoByMemberId($this->site_id, $this->member_id);         switch ($this->channel) {             case ChannelDi...
const member = this.coreMemberService.getInfoByMemberId(this.site_id, this.member_id);
        switch (this.channel) {
            case ChannelDict.WECHAT://公众号
                const openid = openid ? openid : member.wx_openid || '';
                break;
            case ChannelDict.WEAPP://微信小程序
                const openid = openid ? openid : member.weapp_openid || '';
                break;
  }

  /**
   * close
   * 对应 PHP: PayService_api::close()
   * 逻辑类型: undefined - undefined
   */
  async close(type: string, out_trade_no: string) {
    // 基于PHP真实逻辑: close
    // PHP原文: return $this->core_pay_service->close($this->site_id, $out_trade_no);     }      /**      * 支付异步通知      * @param string $channel      * @param string ...
return this.core_pay_service.close(this.site_id, out_trade_no);
    }

    /**
     * 支付异步通知
     * @param string channel
     * @param string type
     * @param string action
     * @return void|null
     */
    async notify(string channel, string type, string action){
        return this.core_pay_service.notify(this.site_id, channel, type, action);
    }

    /**
     * 通过交易流水号查询支付信息以及支付方式
     * @param out_trade_no
     * @return */
    async getInfoByOutTradeNo(out_trade_no){
        return this.core_pay_service.getInfoByOutTradeNo(this.site_id, out_trade_no, this.channel);
    }

    async getInfoByTrade(string trade_type, int trade_id, data){
        return this.core_pay_service.getInfoByTrade(this.site_id, trade_type, trade_id, this.channel, data.scene);
    }

    /**
     * 获取找朋友帮忙付支付信息
     * @param string trade_type
     * @param int trade_id
     * @return */
    async getFriendspayInfoByTrade(trade_type, trade_id){
        const from_pay_info = ( this.payService ).field('site_id').where([ [ 'trade_type', '=', trade_type ], [ 'trade_id', '=', trade_id ) )).findOrEmpty().toArray(];//查询发起交易所属的站点id
        if (!from_pay_info)) throw new ApiBusinessException('TRADE_NOT_EXIST');
        if (from_pay_info.site_id != this.site_id) throw new ApiBusinessException('TRADE_NOT_EXIST');//不是同一站点的用户不能帮付
        const pay_info = this.core_pay_service.getInfoByTrade(from_pay_info[ 'site_id' ), trade_type, trade_id, this.channel, PaySceneDict.FRIENDSPAY];
        if (!!pay_info)) {
            //todo 查询订单交易信息，其它插件可实现该钩子
            const trade_info = array_values(array_filter(event('PayTradeInfo',[ trade_type: trade_type, trade_id: trade_id ))))[0] || [];
            pay_info.trade_info = trade_info;
            if (pay_info.from_main_id != this.member_id) {
                pay_info.is_self = false;
            } } } else {
                pay_info.is_self = true;
            }
            //海报
            const poster = ( this.posterService ).field('id').where([
                [ 'site_id', '=', this.site_id ],
                [ 'type', '=', 'friendspay' ],
                [ 'status', '=', 1 ],
                [ 'is_default', '=', 1 )
            )).findOrEmpty().toArray(];
            if (!!poster)) {
                pay_info.poster_id = poster.id;
            }
            //发起帮付会员信息
            const member = ( this.memberService ).field('member_id,nickname,headimg').where([
                [ 'site_id', '=', this.site_id ],
                [ 'member_id', '=', pay_info.from_main_id )
            )).findOrEmpty().toArray(];
            pay_info.member = member;
  }

  /**
   * notify
   * 对应 PHP: PayService_api::notify()
   * 逻辑类型: undefined - undefined
   */
  async notify(channel: string, type: string, action: string) {
    // 基于PHP真实逻辑: notify
    // PHP原文: return $this->core_pay_service->notify($this->site_id, $channel, $type, $action);     }      /**      * 通过交易流水号查询支付信息以及支付方式      * @param $out_trade_n...
return this.core_pay_service.notify(this.site_id, channel, type, action);
    }

    /**
     * 通过交易流水号查询支付信息以及支付方式
     * @param out_trade_no
     * @return */
    async getInfoByOutTradeNo(out_trade_no){
        return this.core_pay_service.getInfoByOutTradeNo(this.site_id, out_trade_no, this.channel);
    }

    async getInfoByTrade(string trade_type, int trade_id, data){
        return this.core_pay_service.getInfoByTrade(this.site_id, trade_type, trade_id, this.channel, data.scene);
    }

    /**
     * 获取找朋友帮忙付支付信息
     * @param string trade_type
     * @param int trade_id
     * @return */
    async getFriendspayInfoByTrade(trade_type, trade_id){
        const from_pay_info = ( this.payService ).field('site_id').where([ [ 'trade_type', '=', trade_type ], [ 'trade_id', '=', trade_id ) )).findOrEmpty().toArray(];//查询发起交易所属的站点id
        if (!from_pay_info)) throw new ApiBusinessException('TRADE_NOT_EXIST');
        if (from_pay_info.site_id != this.site_id) throw new ApiBusinessException('TRADE_NOT_EXIST');//不是同一站点的用户不能帮付
        const pay_info = this.core_pay_service.getInfoByTrade(from_pay_info[ 'site_id' ), trade_type, trade_id, this.channel, PaySceneDict.FRIENDSPAY];
        if (!!pay_info)) {
            //todo 查询订单交易信息，其它插件可实现该钩子
            const trade_info = array_values(array_filter(event('PayTradeInfo',[ trade_type: trade_type, trade_id: trade_id ))))[0] || [];
            pay_info.trade_info = trade_info;
            if (pay_info.from_main_id != this.member_id) {
                pay_info.is_self = false;
            } } } else {
                pay_info.is_self = true;
            }
            //海报
            const poster = ( this.posterService ).field('id').where([
                [ 'site_id', '=', this.site_id ],
                [ 'type', '=', 'friendspay' ],
                [ 'status', '=', 1 ],
                [ 'is_default', '=', 1 )
            )).findOrEmpty().toArray(];
            if (!!poster)) {
                pay_info.poster_id = poster.id;
            }
            //发起帮付会员信息
            const member = ( this.memberService ).field('member_id,nickname,headimg').where([
                [ 'site_id', '=', this.site_id ],
                [ 'member_id', '=', pay_info.from_main_id )
            )).findOrEmpty().toArray(];
            pay_info.member = member;
  }

  /**
   * getInfoByOutTradeNo
   * 对应 PHP: PayService_api::getInfoByOutTradeNo()
   * 逻辑类型: undefined - undefined
   */
  async getInfoByOutTradeNo(out_trade_no: any) {
    // 基于PHP真实逻辑: getInfoByOutTradeNo
    // PHP原文: return $this->core_pay_service->getInfoByOutTradeNo($this->site_id, $out_trade_no, $this->channel);     }      public function getInfoByTrade(string $...
return this.core_pay_service.getInfoByOutTradeNo(this.site_id, out_trade_no, this.channel);
    }

    async getInfoByTrade(string trade_type, int trade_id, data){
        return this.core_pay_service.getInfoByTrade(this.site_id, trade_type, trade_id, this.channel, data.scene);
    }

    /**
     * 获取找朋友帮忙付支付信息
     * @param string trade_type
     * @param int trade_id
     * @return */
    async getFriendspayInfoByTrade(trade_type, trade_id){
        const from_pay_info = ( this.payService ).field('site_id').where([ [ 'trade_type', '=', trade_type ], [ 'trade_id', '=', trade_id ) )).findOrEmpty().toArray(];//查询发起交易所属的站点id
        if (!from_pay_info)) throw new ApiBusinessException('TRADE_NOT_EXIST');
        if (from_pay_info.site_id != this.site_id) throw new ApiBusinessException('TRADE_NOT_EXIST');//不是同一站点的用户不能帮付
        const pay_info = this.core_pay_service.getInfoByTrade(from_pay_info[ 'site_id' ), trade_type, trade_id, this.channel, PaySceneDict.FRIENDSPAY];
        if (!!pay_info)) {
            //todo 查询订单交易信息，其它插件可实现该钩子
            const trade_info = array_values(array_filter(event('PayTradeInfo',[ trade_type: trade_type, trade_id: trade_id ))))[0] || [];
            pay_info.trade_info = trade_info;
            if (pay_info.from_main_id != this.member_id) {
                pay_info.is_self = false;
            } } } else {
                pay_info.is_self = true;
            }
            //海报
            const poster = ( this.posterService ).field('id').where([
                [ 'site_id', '=', this.site_id ],
                [ 'type', '=', 'friendspay' ],
                [ 'status', '=', 1 ],
                [ 'is_default', '=', 1 )
            )).findOrEmpty().toArray(];
            if (!!poster)) {
                pay_info.poster_id = poster.id;
            }
            //发起帮付会员信息
            const member = ( this.memberService ).field('member_id,nickname,headimg').where([
                [ 'site_id', '=', this.site_id ],
                [ 'member_id', '=', pay_info.from_main_id )
            )).findOrEmpty().toArray(];
            pay_info.member = member;
  }

  /**
   * getInfoByTrade
   * 对应 PHP: PayService_api::getInfoByTrade()
   * 逻辑类型: undefined - undefined
   */
  async getInfoByTrade(trade_type: any[], trade_id: any[], data: any[]) {
    // 基于PHP真实逻辑: getInfoByTrade
    // PHP原文: return $this->core_pay_service->getInfoByTrade($this->site_id, $trade_type, $trade_id, $this->channel, $data['scene']);     }      /**      * 获取找朋友帮忙付...
return this.core_pay_service.getInfoByTrade(this.site_id, trade_type, trade_id, this.channel, data.scene);
    }

    /**
     * 获取找朋友帮忙付支付信息
     * @param string trade_type
     * @param int trade_id
     * @return */
    async getFriendspayInfoByTrade(trade_type, trade_id){
        const from_pay_info = ( this.payService ).field('site_id').where([ [ 'trade_type', '=', trade_type ], [ 'trade_id', '=', trade_id ) )).findOrEmpty().toArray(];//查询发起交易所属的站点id
        if (!from_pay_info)) throw new ApiBusinessException('TRADE_NOT_EXIST');
        if (from_pay_info.site_id != this.site_id) throw new ApiBusinessException('TRADE_NOT_EXIST');//不是同一站点的用户不能帮付
        const pay_info = this.core_pay_service.getInfoByTrade(from_pay_info[ 'site_id' ), trade_type, trade_id, this.channel, PaySceneDict.FRIENDSPAY];
        if (!!pay_info)) {
            //todo 查询订单交易信息，其它插件可实现该钩子
            const trade_info = array_values(array_filter(event('PayTradeInfo',[ trade_type: trade_type, trade_id: trade_id ))))[0] || [];
            pay_info.trade_info = trade_info;
            if (pay_info.from_main_id != this.member_id) {
                pay_info.is_self = false;
            } } } else {
                pay_info.is_self = true;
            }
            //海报
            const poster = ( this.posterService ).field('id').where([
                [ 'site_id', '=', this.site_id ],
                [ 'type', '=', 'friendspay' ],
                [ 'status', '=', 1 ],
                [ 'is_default', '=', 1 )
            )).findOrEmpty().toArray(];
            if (!!poster)) {
                pay_info.poster_id = poster.id;
            }
            //发起帮付会员信息
            const member = ( this.memberService ).field('member_id,nickname,headimg').where([
                [ 'site_id', '=', this.site_id ],
                [ 'member_id', '=', pay_info.from_main_id )
            )).findOrEmpty().toArray(];
            pay_info.member = member;
  }

  /**
   * getFriendspayInfoByTrade
   * 对应 PHP: PayService_api::getFriendspayInfoByTrade()
   * 逻辑类型: undefined - undefined
   */
  async getFriendspayInfoByTrade(trade_type: any, trade_id: any) {
    // 基于PHP真实逻辑: getFriendspayInfoByTrade
    // PHP原文: $from_pay_info = ( new Pay() )->field('site_id')->where([ [ 'trade_type', '=', $trade_type ], [ 'trade_id', '=', $trade_id ] ])->findOrEmpty()->toArra...
const from_pay_info = ( this.payService ).field('site_id').where([ [ 'trade_type', '=', trade_type ], [ 'trade_id', '=', trade_id ) )).findOrEmpty().toArray(];//查询发起交易所属的站点id
        if (!from_pay_info)) throw new ApiBusinessException('TRADE_NOT_EXIST');
        if (from_pay_info.site_id != this.site_id) throw new ApiBusinessException('TRADE_NOT_EXIST');//不是同一站点的用户不能帮付
        const pay_info = this.core_pay_service.getInfoByTrade(from_pay_info[ 'site_id' ), trade_type, trade_id, this.channel, PaySceneDict.FRIENDSPAY];
        if (!!pay_info)) {
            //todo 查询订单交易信息，其它插件可实现该钩子
            const trade_info = array_values(array_filter(event('PayTradeInfo',[ trade_type: trade_type, trade_id: trade_id ))))[0] || [];
            pay_info.trade_info = trade_info;
            if (pay_info.from_main_id != this.member_id) {
                pay_info.is_self = false;
            } } } else {
                pay_info.is_self = true;
            }
            //海报
            const poster = ( this.posterService ).field('id').where([
                [ 'site_id', '=', this.site_id ],
                [ 'type', '=', 'friendspay' ],
                [ 'status', '=', 1 ],
                [ 'is_default', '=', 1 )
            )).findOrEmpty().toArray(];
            if (!!poster)) {
                pay_info.poster_id = poster.id;
            }
            //发起帮付会员信息
            const member = ( this.memberService ).field('member_id,nickname,headimg').where([
                [ 'site_id', '=', this.site_id ],
                [ 'member_id', '=', pay_info.from_main_id )
            )).findOrEmpty().toArray(];
            pay_info.member = member;
  }

  /**
   * getPayTypeByTrade
   * 对应 PHP: PayService_api::getPayTypeByTrade()
   * 逻辑类型: undefined - undefined
   */
  async getPayTypeByTrade(trade_type: string) {
    // 基于PHP真实逻辑: getPayTypeByTrade
    // PHP原文: return $this->core_pay_service->getPayTypeByTrade($this->site_id, $trade_type, $this->channel);     } }...
return this.core_pay_service.getPayTypeByTrade(this.site_id, trade_type, this.channel);
    }
}
  }
}
